//
//  RadiateViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/6.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "RadiateViewController.h"
#import "RadiateView.h"
#import "RadiateKnowledgeViewController.h"
#import "PreventRadiateViewController.h"
#import "XTPopTableView.h"
#import "RadiateSettingViewController.h"
#import "RadiateChartViewController.h"



#import "BlueToothDataHandle.h"


@interface RadiateViewController ()<SelectIndexPathDelegate,timedelegate>

@property(nonatomic,strong) RadiateView *containerView;
@property(nonatomic,strong) UIButton *titleButton;

@end

@implementation RadiateViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBattery) name:BatteryState object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeCirCleAnimation) name:DeviceDisconnect object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotationCirCleAnimation) name:DeviceConnectSuccess object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [BlueToothDataHandle sharedInstance].delegate = self;
    DTCDeviceModel *model = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    if ([model.mac isEqualToString:EXRACT_OBJECT(MAC)])
    {
        [self rotationCirCleAnimation];
    }

}

- (void)hshhshshs:(NSString *)contentMoth andmin:(NSString *)min{
    
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self checkRadiateWithSwitch:YES];
    [self updateBattery];
}

- (void)updateBattery
{
    [self setRightButtonImage:[UIImage imageNamed:[NSString stringWithFormat:@"nav_icon_cell%@",[BlueToothDataHandle sharedInstance].battery]]];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self checkRadiateWithSwitch:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)refreshBaseControlValue
{
    __weak typeof(self) weakSelf = self;
   [BlueToothDataHandle sharedInstance].radiateBlock = ^(NSString *radiate) {
       weakSelf.containerView.radiateLabel.text = radiate;
       CGFloat value = [radiate floatValue];
       if (value >= 0.4) // 危险
       {
           weakSelf.containerView.tipLabel.text = MyNSLocalizedString(@"危险范围值");
           weakSelf.containerView.circleView.image = imageWithName(@"gif_red");
           weakSelf.containerView.unitLabel.textColor = colorWithAlpha(240, 97, 97, 1);
           weakSelf.containerView.radiateLabel.textColor = colorWithAlpha(240, 97, 97, 1);
       }
       else if (value > 0.2 && value < 0.4) // 良好
       {
           weakSelf.containerView.tipLabel.text = MyNSLocalizedString(@"良好范围值");
           weakSelf.containerView.circleView.image = imageWithName(@"gif_yellow");
           weakSelf.containerView.unitLabel.textColor = colorWithAlpha(255, 199, 108, 1);
           weakSelf.containerView.radiateLabel.textColor = colorWithAlpha(255, 199, 108, 1);
       }
       else // 安全
       {
           weakSelf.containerView.tipLabel.text = MyNSLocalizedString(@"安全范围值");
           weakSelf.containerView.circleView.image = imageWithName(@"gif_blue");
           weakSelf.containerView.unitLabel.textColor = colorWithAlpha(107, 202, 249, 1);
           weakSelf.containerView.radiateLabel.textColor = colorWithAlpha(107, 202, 249, 1);
       }
   };
}




#pragma mark - 点击事件
- (void)didClickTitleButton:(id)sender
{
    self.titleButton.selected = !self.titleButton.selected;
    [XTPopTableView showPopViewByCenter:sender valueList:@[MyNSLocalizedString(@"统计"),MyNSLocalizedString(@"分享")] imageList:@[@"icon_statis",@"icon_share"] delegate:self];
}
- (void)didClickRadiateKnowledgeButton
{
    RadiateKnowledgeViewController *vc = [[RadiateKnowledgeViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didClickPreventRadiateButton
{
    PreventRadiateViewController *vc = [[PreventRadiateViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 事件处理
- (void)checkRadiateWithSwitch:(BOOL)isOn
{
    NSString *code = [[NSString alloc] init];
    if (isOn)
    {
        code = startCheckRadiateCode;
    }
    else
    {
        code = stopCheckRadiateCode;
    }
    DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    NSData *sendData = [NSString convertHexStrToData:code];
    [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
}

- (void)removeCirCleAnimation
{
    [self.containerView.circleView.layer removeAllAnimations];
}

- (void)rotationCirCleAnimation
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 2;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1000;
    [self.containerView.circleView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

#pragma mark - 代理
- (void)selectIndexPathRow:(NSInteger )index
{
    switch (index) {
        case 0:
        {
            RadiateChartViewController *vc = [[RadiateChartViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            RadiateSettingViewController *vc = [[RadiateSettingViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            UIImage *screenShot = [OTScreenshotHelper screenshot];
            [ShareView showShareViewWithClickShareBlock:^(ShareType shareType) {
                [DTCSharePlatformManager shareWithImage:screenShot Type:shareType];
            }];
        }
            break;
            
        default:
            break;
    }
}

- (void)closePopView
{
    self.titleButton.selected = !self.titleButton.selected;
}

#pragma mark - 通知处理

#pragma mark - 初始化
- (RadiateView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[RadiateView alloc]init];
        [superView addSubview:_containerView];
        [_containerView.radiateKnowledgeButton addTarget:self action:@selector(didClickRadiateKnowledgeButton) forControlEvents:UIControlEventTouchUpInside];
        [_containerView.preventRadiateButton addTarget:self action:@selector(didClickPreventRadiateButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _containerView;
}

- (UIButton *)titleButton
{
    if(!_titleButton)
    {
        UIView *superView = self.view;
        _titleButton = [[UIButton alloc]init];
        [superView addSubview:_titleButton];
        [_titleButton setTitle:MyNSLocalizedString(@"辐射") forState:UIControlStateNormal];
        [_titleButton setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
        [_titleButton setImage:[UIImage imageNamed:@"up"] forState:UIControlStateSelected];
        [_titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_titleButton sizeToFit];
        [_titleButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
        [_titleButton addTarget:self action:@selector(didClickTitleButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _titleButton;
}


- (void)setNavigationStyle
{
    [self setTitleView:self.titleButton];
    
    [self setRightButtonImage:[UIImage imageNamed:@"nav_icon_cell1"]];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.right.bottom.equalTo(superView);
    }];
    
}

@end
